package com.hzdh.account.controller.account;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hzdh.account.commom.Message;
import com.hzdh.account.commom.validate.AjaxResponse;
import com.hzdh.account.entity.express.ToExpressCompany;
import com.hzdh.account.entity.pay.ToDayBook;
import com.hzdh.account.entity.pay.ToType;
import com.hzdh.account.request.account.ToDayBookRequest;
import com.hzdh.account.response.account.list.ToAccountListResponse;
import com.hzdh.account.service.AccountService;
import com.hzdh.account.service.TypeService;
import com.hzdh.account.utils.DateUtils;

@Controller
@RequestMapping("/account")
public class AccountListController {

	private static Logger logger = LoggerFactory.getLogger(AccountListController.class);
	
	private static String PLAT_TM = "tm";
	private static String PLAT_PDD = "pdd";
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private TypeService typeService;
	
	@RequestMapping("/list")
	public String accountView(HttpServletRequest request) {
		
		return "account/list";
	}
	
	@RequestMapping("/add/base/info")
	public String accountAddView(HttpServletRequest request, String resource) {
		//日常编码 GD-TM-20210424
		StringBuffer sb = null;
		if (PLAT_TM.equals(resource)) {
			sb = new StringBuffer("GD-TM-");
		} else if (PLAT_PDD.equals(resource)) {
			sb = new StringBuffer("GD-PDD-");
		}
		Date date = new Date();
		String dateToString = DateUtils.getFormatDate(date, "yyyyMMdd");
		String dayCode = sb.append(dateToString).toString();
		request.setAttribute("dayCode", dayCode);
		request.setAttribute("resource", resource);
		return "account/base/add";
	}
	
	/**
	 * 保存流水账单数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public AjaxResponse saveAccountPay(ToDayBookRequest toDayBookRequest) {
		AjaxResponse res = new AjaxResponse();
		try {
			accountService.saveToDayBookInfo(toDayBookRequest);
			res.success();
		} catch (Exception e) {
			e.getStackTrace();
			System.out.println(e.getMessage());
			logger.info("" , e.getStackTrace());
			res.setContent("保存流水账户信息异常!");
			res.fail();
		}
		return res;
	}
	
	/**
	 * 流水列表总览分页查询
	 * @param paramMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query/account/list")
	public Message queryAccountList(@RequestParam Map<String, String> paramMap) {
		
		Message message = null;
		try {
			PageInfo<ToDayBook> pageInfo = accountService.getToDayBookPage(paramMap);
			if (pageInfo != null) {
				if (pageInfo.getTotal() > 0) {
					message = new Message<>(0, "获取数据成功!", pageInfo.getTotal(), pageInfo.getList());
				} else {
					message = new Message<>(0, "无数据!", null, null);
				}
			} else {
				message = new Message<>(0, "参数异常!", null, null);
			}
		} catch (Exception e) {
			logger.info("exception = {}", e.getStackTrace() + e.getMessage());
			message = new Message(400,"查询失败!",0L,null);
		}
		return message;
	}
	
	
	@ResponseBody
	@RequestMapping("/query/list")
	public Message queryList(@RequestParam Map<String, String> paramMap) {
		Message message = null;
		try {
			PageInfo<ToAccountListResponse> pageInfo = accountService.getAccountListPage(paramMap);
			if (pageInfo != null) {
				if (pageInfo.getTotal() > 0) {
					message = new Message<>(0, "获取数据成功!", pageInfo.getTotal(), pageInfo.getList());
				} else {
					message = new Message<>(0, "无数据!", null, null);
				}
			} else {
				message = new Message<>(0, "参数异常!", null, null);
			}
		} catch (Exception e) {
			logger.info("exception = {}", e.getStackTrace() + e.getMessage());
			message = new Message(400,"查询失败!",0L,null);
		}
		return message;
	}
	
	
	/**
	 * 查询快递公司信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query/express/company")
	public Object queryExpressCompany() {
		List<ToExpressCompany> expressCompanys = accountService.getToExpressCompany();
		return expressCompanys;
	}

	/**
	 * 查询支出类型信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query/type/name")
	public Object queryTypeName() {
		List<ToType> types = typeService.getToTypeList();
		return types;
	}
	
	
}
